#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n, k;
map<int, int>mp;
int a[N];
int f1[N], f2[N], f3[N];
int main()
{
	int t;
	cin >> t;
	while (t--) {
		scanf("%d%d", &n, &k);
		for (int i = 0; i <= n; i++) {
			a[i] = 0;
			f1[i] = f2[i] = f3[i] = 0;
		}
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			//if (!mp[a[i]]) {
			//	mp[a[i]] = 1;
			//	f3[a[i]] = i;
			//	//f1[a[i]] = i;
			//}
		}
		/*for (int i = 1; i <= n; i++) {
			cout << "f3[ai]==" << f3[a[i]] << " ";
		}
		cout << endl;*/
		for (int i = 1; i <= n; i++) {
			int x = i - f3[a[i]] - 1;
			//if(a[i]==4) cout << "x==" << x << endl;
			if (x >= f1[a[i]]) {
				f2[a[i]] = f1[a[i]];
				f1[a[i]] = x;
			}
			else if (x >= f2[a[i]]) {
				f2[a[i]] = x;
			}
			f3[a[i]] = i;
		}
		for (int i = 1; i <= k; i++) {
			int x = n + 1 - f3[i] - 1;
			if (x >= f1[i]) {
				f2[i] = f1[i];
				f1[i] = x;
			}
			else if (x >= f2[i]) {
				f2[i] = x;
			}
		}
		int ans = 1e9;
		for (int i = 1; i <= k; i++) {
			/*cout << "f1==" << f1[i] << endl;
			cout << "f2==" << f2[i] << endl;
			cout << "f3==" << f3[i] << endl;*/
			ans = min(ans, max(f1[i] / 2, f2[i]));
			//cout << "ans==" << ans << endl;
		}
		printf("%d\n", ans);
	}
	return 0;
}